package com.yyj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyj.dao.UserDao;
import com.yyj.entity.*;
import com.yyj.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserService {
    @Autowired
    UserDao userDao;
    public List<Pet> getDog(PetParams petParams) {
        List<Pet> list = userDao.getDog(petParams);
        return list;
    }
    public PageInfo<User> getBySearch(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //裸数据 不包括数据总数,当前页和页面大小
        List<User> list = userDao.getBySearch(params);
        //封装在一起的数据
        return PageInfo.of(list);
    }
    public void insertUser(User user) {

        userDao.insertUser(user);
    }
    public void updateUser(User user) {
        userDao.updateUser(user);
    }
    public void deleteUser(Integer UserId) {
        userDao.deleteByPrimaryKey(UserId);
    }

    public User login(User user) {
        if (user.getName() == null || user.getName().isEmpty()) {
            throw new CustomException("用户名不能为空");
        }
        if (user.getPassword() == null || user.getPassword().isEmpty()) {
            throw new CustomException("密码不能为空");
        }

        User loginUser = userDao.login(user);
        if (loginUser == null) {
            throw new CustomException("用户名或密码错误");
        }
        return loginUser;
    }
    public void add(User user) {
        if (user.getName() == null ||"" .equals(user.getName())) {
            throw new CustomException("用户名不能为空");
        }
        //添加管理员的重复性判断
        User user1 = userDao.findByName(user.getName());
        if (user1 != null) {
            throw new CustomException("该用户名已经存在，请更换");
        }
        if(user.getPassword()== null){
            user.setPassword("123456");
            //已经有了，提示前台

        }
    userDao.insert(user);

    }
    public void insertDog(Pet pet) {
        userDao.insertDog(pet);
    }

    public User findById(Integer id) {
        return userDao.findById(id);
    }

    public void update(Pet pet) {
        userDao.update(pet);
    }

    public void delete(Integer petId) {
        userDao.delete(petId);
    }

    public void updateImg(User user) {
        userDao.updateImg(user);
    }

    public void updatePwd(Integer userId) {
        userDao.updatePwd(userId);
    }

    public List<Pet> findByNewOwner(Integer newOwner) {
        List<Pet> list = userDao.findByNewOwner(newOwner);
        return list;
    }
}
